const { Model, DataTypes } = require('sequelize')
const { createFields } = require('../helper')
module.exports = (sequelize) => {
  class Menu extends Model {
    static associate({ Role, System, RoleMenu }) {
      Menu.belongsToMany(Role, {
        as: 'roleList',
        through: 'sys_role_menu',
        constraints: false,
      })

      Menu.belongsTo(System, {
        as: 'system',
        constraints: false,
        foreignKey: 'systemId',
        targetKey: 'id',
      })
    }
  }
  Menu.init(
    createFields({
      name: { type: DataTypes.STRING, comment: '菜单名称' },
      code: { type: DataTypes.STRING, comment: '菜单编码' },
      description: {
        type: DataTypes.STRING(255),
        comment: '角色描述',
        allowNull: true,
      },
      url: { type: DataTypes.STRING, comment: '菜单链接', allowNull: true },
      icon: { type: DataTypes.STRING, comment: '图标', allowNull: true },
      type: {
        type: DataTypes.STRING,
        comment: '菜单类型',
        defaultValue: 'menu',
      },
      parentId: {
        type: DataTypes.STRING(20),
        defaultValue: 0,
        comment: '父元素id，0为根',
      },
      sort: {
        type: DataTypes.INTEGER,
        defaultValue: 0,
        column: '排序',
      },
      systemId: {
        type: DataTypes.STRING,
        comment: '系统id',
      },
    }),
    {
      sequelize,
      tableName: 'sys_menu',
      timestamps: true,
      modelName: 'Menu',
    }
  )

  return Menu
}
